package com.c.ctgproject.ui.files

import android.view.View
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.file.FileFormalitiesManagerEntity
import com.c.ctgproject.entity.file.FileManagerDetailEntity
import com.c.ctgproject.ui.files.callback.FileManagerDetailViewModel
import com.c.ctgproject.utils.*
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo
import kotlinx.android.synthetic.main.activity_file_manager_detail.*
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource

/**
 *作者:LiBW
 *创建日期: 2021/7/6
 *描述: 档案管理 页面 (详情)
 */
class FileManagerDetailActivity : BaseActivity() {
    private val mViewModel by lazy { ViewModelProvider(this)[FileManagerDetailViewModel::class.java] }
    private val carID by lazy { intent.getStringExtra(MyConstants.CAR_ID).handlerNull() }
    private val mData by lazy { intent.getSerializableExtra(MyConstants.DATA) as FileFormalitiesManagerEntity.DataBean.ListBean }
    override fun getLayout() = R.layout.activity_file_manager_detail


    override fun event() {
        mViewModel.detail(carID)
        mCarPlateVIEW.textSet(mData.ownerPlate.handlerNull())
        mOwnerVIEW.textSet(mData.carOwner.handlerNull())
        mCarVinVIEW.textSet(mData.carVin.handlerNull())
        mOwnerPhoneVIEW.textSet(mData.ownerPhone.handlerNull())
        mSalesmanTV.textSet(mData.salesman.handlerNull())
        mInTimeTV.textSet(mData.enterTime.handlerNull())
        mBecauseET.textSet(mData.remark.handlerNull())
        mViewModel.error.observe(this, {
            MyLog.d("错误:$it")
            showToast(it)
        })

        mViewModel.result.observe(this, {
            MyLog.d("档案管理详情:$it")
            handlerHttpResult(it) {
                val data = getGson().fromJson(it, FileManagerDetailEntity::class.java).data
                data?.let { bean ->
                    //是否保号
                    when (bean.doesGuaranteed) {
                        "1" -> {
                            mDoesGuaranteedView.textSet("是")
                        }
                        "0" -> {
                            mDoesGuaranteedView.textSet("否")
                        }
                        else -> {
                            mDoesGuaranteedView.textSet("-")
                        }
                    }
                    //车型
                    mCarTypeTV.textSet(data.carModel.handlerNull())
                    //一验
                    val firstCheck = when (data.firstCheck) {
                        "1" -> {
                            "车辆一验✅:"
                        }
                        "0" -> {
                            "车辆一验❌:"
                        }
                        else -> "车辆一验"
                    }
                    mCarFirstCheckTV.setStartTV(firstCheck)
                    mCarFirstCheckTV.textSet(data.firstRemark.handlerNull())
                    //二验
                    val secondCheck = when (data.secondCheck) {
                        "1" -> {
                            "车辆二验✅:"
                        }
                        "0" -> {
                            "车辆二验❌:"
                        }
                        else -> "车辆二验:"
                    }
                    if (data.carSign=="1"){
                        hasProblem=true
                        mSwitch.isChecked=true
                    }

                    mCarSecondCheckTV.setStartTV(secondCheck)
                    mCarSecondCheckTV.textSet(data.secondRemark.handlerNull())
                    mReceiverNameTV.textSet(data.recipient.handlerNull())
                    shoujianren = data.recipient.handlerNull()
                    shoujianrendianhua = data.recipientPhone.handlerNull()
                    shoujianrendizhi = data.mailingAddress.handlerNull()
                    mReceiverPhoneTV.textSet(data.recipientPhone.handlerNull())
                    mReceiveAddressTV.textSet(data.mailingAddress.handlerNull())


                    if (data.drivingLicense == "1") {
                        xingshi = true
                        changeCheck(mxingshi, true)
                    }
                    if (data.registration == "1") {
                        dengji = true
                        changeCheck(mdengji, true)
                    }
                    if (data.idCardCopy == "1") {
                        shenfenzheng = true
                        changeCheck(mshenfenzheng, true)
                    }
                    if (data.notice == "1") {
                        gaozhi = true
                        changeCheck(mgaozhi, true)
                    }
                    if (data.attorneyPaper == "1") {
                        weituo = true
                        changeCheck(mweituo, true)
                    }
                    if (data.plateApplicationForm == "1") {
                        shenlingbiao = true
                        changeCheck(mshenlingbiao, true)
                    }
                }
            }
        })
        mViewModel.submitResult.observe(this, {
            MyLog.d("提交回调:$it")
            handlerHttpResult(it) {
                showToast("提交成功")
                finish()
            }
        })
    }

    private fun changeCheck(view: TextView, select: Boolean = false) {
        if (select) {
            view.backgroundResource = R.drawable.bg_border_acc_5
            view.textColorResource = R.color.colorAccent
        } else {
            view.backgroundResource = R.drawable.bg_border_gray_5
            view.textColorResource = R.color.gray_text
        }
    }

    //车辆是否有问题
    private var hasProblem = false
    private var xingshi = false
    private var dengji = false
    private var weituo = false
    private var shenfenzheng = false
    private var shenlingbiao = false
    private var gaozhi = false
    private var shoujianren = ""
    private var shoujianrendianhua = ""
    private var shoujianrendizhi = ""

    override fun click() {
        //视频和照片
        mPhotoImageIV.setOnClickListener {
            startActivity<FileImageVideoActivity>(MyConstants.CAR_ID to mData.carId.handlerNull())
        }
        mxingshi.setOnClickListener {
            xingshi = !xingshi
            changeCheck(mxingshi, xingshi)
           it.anim()

        }
        mgaozhi.setOnClickListener {
            gaozhi = !gaozhi
            changeCheck(mgaozhi, gaozhi)
            it.anim()
        }
        mdengji.setOnClickListener {
            dengji = !dengji
            changeCheck(mdengji, dengji)
            it.anim()
        }
        mweituo.setOnClickListener {
            weituo = !weituo
            changeCheck(mweituo, weituo)
            it.anim()
        }
        mshenfenzheng.setOnClickListener {
            shenfenzheng = !shenfenzheng
            changeCheck(mshenfenzheng, shenfenzheng)
            it.anim()
        }
        mshenlingbiao.setOnClickListener {
            shenlingbiao = !shenlingbiao
            changeCheck(mshenlingbiao, shenlingbiao)
            it.anim()
        }
        //问题车辆
        mSwitch.setOnCheckedChangeListener { _, isChecked ->
            hasProblem = isChecked
        }
        mViewModel.remarkResult.observe(this,{
            MyLog.d("提交备注回调:$it")
        })

        //提交
        mSubmitBTN.setOnClickListener {
            MyLog.d("提交数据车ID:${mData.carId.handlerNull()}")
            MyLog.d("提交数据备注:${mBecauseET.text.handlerNull()}")
            mViewModel.submitRemark(mData.carId.handlerNull(),mBecauseET.text.handlerNull())

            mViewModel.submit(
                handlerBody(
                    "carId" to mData.carId.handlerNull(),
                    "carSign" to if (hasProblem) "1" else "2",
                    "drivingLicense" to if (xingshi) "1" else "0",
                    "registration" to if (dengji) "1" else "0",
                    "idCardCopy" to if (shenfenzheng) "1" else "0",
                    "notice" to if (gaozhi) "1" else "0",
                    "attorneyPaper" to if (weituo) "1" else "0",
                    "plateApplicationForm" to if (shenlingbiao) "1" else "0",
                    "recipient" to shoujianren,
                    "recipientPhone" to shoujianrendianhua,
                    "mailingAddress" to shoujianrendizhi,
                )
            )
        }
    }
    private fun View.anim(){
        YoYo.with(Techniques.Pulse).duration(100).playOn(this)
    }
}